import { message } from 'antd';
import router from 'umi/router';
import {
  getIssProveList,
  deleteIsssProveData,
  getDetail,
  issueproveAddCheck,
  issueproveAdd,
  issueproveUpdate,
} from '@/services/isseprove';
import { getEmpNameList } from '@/services/searchrecord';

export default {
  state: {
    formFieldsValue: {},
    expandForm: false,
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    empNameList: [],
    updateDetail: { hdlWay: undefined, hdlFlag: '1', state: '1' },
    fileListOld: [],
    fileListEmlOld: [],
    fileListExprsOld: [],
    fileListHandOld: [],
  },
  effects: {
    // 搜索姓名
    *fetchEmpNameList({ payload }, { call, put }) {
      const response = yield call(getEmpNameList, payload);
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'getEmpNameList',
            payload: response.data,
          });
        }
        if (!response.data.length) {
          message.warning('当前所输的不存在，请检查 ');
        }
      }
      return response;
    },
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getIssProveList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'updateList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 删除
    *deleteData({ payload }, { call, put }) {
      const response = yield call(deleteIsssProveData, payload);
      const values = { pageArg: { currentPage: 1, pageSize: 10 } };
      if (response.success) {
        yield put({
          type: 'fetchList',
          payload: values,
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取详情
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(getDetail, payload);
      if (response.success) {
        yield put({
          type: 'getDetail',
          payload: response.data,
        });
        return response.data;
      }
      return message.warning(response.message || '服务端错误！');
    },
    // 获取修改页详情
    *fetchUpdateDetail({ payload }, { call, put }) {
      const response = yield call(getDetail, payload);
      if (response.success) {
        yield put({
          type: 'getUpdateDetail',
          payload: response.data,
        });
        return response.data;
      }
      return message.warning(response.message || '服务端错误！');
    },
    // 提交校验
    *featchCheckIssueProveAdd({ payload }, { call }) {
      const response = yield call(issueproveAddCheck, payload);
      if (response.success) {
        return response;
      }
      return message.warning(response.message || '服务端错误！');
    },
    // 提交
    featchIssueProveAdd:[
      function *({ payload }, { call }) {
        const response = yield call(issueproveAdd, payload);
        if (response.success) {
          router.push('/hrmanage/issueproof/index');
        } else {
          message.warning(response.message || '服务端错误！');
        }
      },
      { type:'throttle' , ms :10000 }
    ],
    // 修改
    featchIssueProveUpdate:[
      function* ({ payload }, { call }) {
        const response = yield call(issueproveUpdate, payload);
        if (response.success) {
          router.goBack();
        } else {
          message.warning(response.message || '服务端错误！');
        }
      },
      { type:'throttle' , ms :10000 }
    ]
  },

  reducers: {
    // 人员回显
    getEmpNameList(state, { payload }) {
      const empData = Array.isArray(payload) ? payload : [];
      return {
        ...state,
        empNameList: empData,
      };
    },
    // 获取列表
    updateList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // dom显示隐藏
    changeDomShow(state, { payload }) {
      return {
        ...state,
        updateDetail: {
          ...state.updateDetail,
          hdlWay: payload,
        },
      };
    },

    changeChildDomShow(state, { payload }) {
      return {
        ...state,
        updateDetail: {
          ...state.updateDetail,
          hdlFlag: payload,
        },
      };
    },

    getDetail(state, { payload }) {
      const newData = payload;
      Object.keys(payload).forEach(key => {
        if (key === 'exprsAffix' || key === 'emlAffix' || key === 'affix') {
          newData[key] = payload[key] ? payload[key].split(',') : [];
        }
      });
      return {
        ...state,
        ...newData,
      };
    },

    getUpdateDetail(state, { payload }) {
      let fileListEmlOld = [];
      let fileListExprsOld = [];
      let fileListHandOld = [];
      Object.keys(payload).forEach(key => {
        if (key === 'emlAffix') {
          const newObj = payload[key].split(',');
          fileListEmlOld = newObj.map(item => ({
            uid: item.split('|')[0],
            name: item.split('|')[1],
          }));
        }
        if (key === 'exprsAffix') {
          const newObj = payload[key].split(',');
          fileListExprsOld = newObj.map(item => ({
            uid: item.split('|')[0],
            name: item.split('|')[1],
          }));
        }
        if (key === 'affix') {
          const newObj = payload[key].split(',');
          fileListHandOld = newObj.map(item => ({
            uid: item.split('|')[0],
            name: item.split('|')[1],
          }));
        }
      });

      return {
        ...state,
        updateDetail: payload,
        fileListExprsOld,
        fileListEmlOld,
        fileListHandOld,
      };
    },

    // 新增上传文件
    updateEmlData(state, { payload }) {
      if (payload.length) {
        state.fileListEmlOld.push({ uid: payload[0].uid, name: payload[0].name });
      }
      return {
        ...state,
      };
    },
    updateEmlDele(state, { payload }) {
      return {
        ...state,
        fileListEmlOld: state.fileListEmlOld.filter(item => item.uid !== payload.uid),
      };
    },
    // 新增上传文件
    updateExprsData(state, { payload }) {
      if (payload.length) {
        state.fileListExprsOld.push({ uid: payload[0].uid, name: payload[0].name });
      }
      return {
        ...state,
      };
    },
    updateExprsDele(state, { payload }) {
      return {
        ...state,
        fileListExprsOld: state.fileListExprsOld.filter(item => item.uid !== payload.uid),
      };
    },

    // 新增上传文件
    updateHandData(state, { payload }) {
      if (payload.length) {
        state.fileListHandOld.push({ uid: payload[0].uid, name: payload[0].name });
      }
      return {
        ...state,
      };
    },

    updateHandDele(state, { payload }) {
      return {
        ...state,
        fileListHandOld: state.fileListHandOld.filter(item => item.uid !== payload.uid),
      };
    },
    updateModelEmail(state, { payload }) {
      const newState = state;
      newState.updateDetail.recEml = payload.updateDetail.recEml;
      return {
        ...state,
      };
    },
  },
};
